﻿/**********************************************
 * 功能名称：作业中心定义
编写人：  杨莹
建立时间： 杨 2008年11月15日
说明：
加载页面时，列出作业中心树型目录
可点击相应的树结点，进行编辑、删除
可在当前树结点增加子节点。
 **************************************************************/

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.Controllers;

using System.Data.Linq;
using System.Collections.Generic;

namespace Hisun.View.CostCenterSetup
{
    public partial class CostCenterSetup : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CreateTree();
                BindDropDownList_CosterItems();


            }
            EditState(false);
            Button_Del.Attributes["onClick"] = "return confirm('真的要删除吗？');";
        }

        #region 生成作业中心目录数
        private void MakeMenu(TABC_JS_CostCenter[] menuSource)
        {
            foreach (var node in menuSource)
            {
                // 创建父节点
                if (node.上级作业中心ID == 0)
                {
                    TreeNode newNode = new TreeNode(node.作业中心名称, node.作业中心ID.ToString());
                    // newNode.NavigateUrl = node.页面文件;
                    this.TreeView_CenterTree.Nodes.Add(newNode);
                    GenerateSubMenu(GetSubNodeSource(node, menuSource), newNode, menuSource);
                }
            }
            TreeView_CenterTree.ExpandAll();
        }


        /// <summary>
        /// 产生子节点
        /// </summary>
        /// <param name="childNodes"></param>
        /// <param name="fatherNode"></param>
        /// <param name="menuSource"></param>
        private void GenerateSubMenu(TABC_JS_CostCenter[] childNodes, TreeNode fatherNode, TABC_JS_CostCenter[] menuSource)
        {
            foreach (TABC_JS_CostCenter node in childNodes)
            {
                TreeNode newNode = new TreeNode( node.作业中心名称, node.作业中心ID.ToString());
                // newNode.NavigateUrl = node.页面文件;
                newNode.ToolTip = "作业中心ID[" + node.作业中心ID.ToString() + "]";

                fatherNode.ChildNodes.Add(newNode);

                GenerateSubMenu(GetSubNodeSource(node, menuSource), newNode, menuSource);
            }
        }
        /// <summary>
        /// 产生子节点的数据源
        /// </summary>
        /// <param name="father"></param>
        /// <param name="menuSource"></param>
        /// <returns></returns>
        private TABC_JS_CostCenter[] GetSubNodeSource(TABC_JS_CostCenter father, TABC_JS_CostCenter[] menuSource)
        {
            List<TABC_JS_CostCenter> subNodesSource = new List<TABC_JS_CostCenter>();
            foreach (TABC_JS_CostCenter node in menuSource)
            {
                //if (father.是否末级 == 'T')
                //{
                //    break;
                //}
                //else
                //{
                if (node.上级作业中心ID == father.作业中心ID)
                {
                    subNodesSource.Add(node);
                }
                //}
            }
            return subNodesSource.ToArray();
        }
        #endregion


        private void BindDropDownList_CosterItems()
        {
            var MyBindItems = from a in db.TABC_DM_CostItems
                              select new
                              {
                                  a.成本细项ID,
                                  a.成本细项名称
                              };

            DropDownList_CostItems.Items.Clear();
    
            ListItem  myItem = new ListItem();
            myItem.Value = string.Empty;
            myItem.Text = "无";
            DropDownList_CostItems.Items.Add(myItem);
            ListItem myItem1 = new ListItem();

            int i = 0;
            ListItem[] myintem = new ListItem[MyBindItems.Count()];
            foreach (var mybinditem in MyBindItems)
            {

                myintem[i]=new ListItem() ;

                myintem[i].Value = mybinditem.成本细项ID;
                myintem[i].Text = mybinditem.成本细项名称;
                DropDownList_CostItems.Items.Add(myintem[i]);
            
             i++;
            
            }





        }

        //当选择的行改变的时候，填充右边的编辑栏 
        protected void TreeView_CenterTree_SelectedNodeChanged(object sender, EventArgs e)
        {

            int MyCenterId = Convert.ToInt32(TreeView_CenterTree.SelectedNode.Value);

            BindMyData(MyCenterId);

            EditFlag.Value = "Browse";
            EditState(false);


        }

        private void CreateTree()
        {
            var MyCenterTree = (from a in db.TABC_JS_CostCenter select a).ToArray();
            this.TreeView_CenterTree.Nodes.Clear();
            MakeMenu(MyCenterTree);


        }

        private void BindMyData(int MyCenterId)
        {

            var MyCenterQuery = (from a in db.TABC_JS_CostCenter where a.作业中心ID == MyCenterId select a).Single();

            Label_CenterID.Text = MyCenterId.ToString();
            TextBox_FatherId.Text = MyCenterQuery.上级作业中心ID.ToString();
            TextBox_Name.Text = MyCenterQuery.作业中心名称;
            TextBox_Description.Text = MyCenterQuery.作业中心描述;
            DropDownList_Sort.SelectedValue = MyCenterQuery.类型;
            CheckBox_DirectCost.Checked = Convert.ToBoolean(MyCenterQuery.是否直接成本);
            CheckBox_StopFlag.Checked = Convert.ToBoolean(MyCenterQuery.停用);
            TextBox_Organ.Text = MyCenterQuery.部门ID;
            string xx = MyCenterQuery.成本细项ID;
            DropDownList_CostItems.SelectedValue = MyCenterQuery.成本细项ID;


           



        }


        //当处于新增、编辑状态的时候，改变所有需要录入数据的可控属性为TRUE，反之为FALSE
        private void EditState(bool EditState)
        {
            if (EditState)
            {

                TextBox_FatherId.Enabled = true;
                TextBox_Name.Enabled = true;
                TextBox_Description.Enabled = true;
                DropDownList_Sort.Enabled = true;
                CheckBox_DirectCost.Enabled = true;
                CheckBox_StopFlag.Enabled = true;
                TextBox_Organ.Enabled = true;
                DropDownList_CostItems.Enabled = true;


            }
            else
            {

                TextBox_FatherId.Enabled = false;
                TextBox_Name.Enabled = false;
                TextBox_Description.Enabled = false;
                DropDownList_Sort.Enabled = false;
                CheckBox_DirectCost.Enabled = false;
                CheckBox_StopFlag.Enabled = false;
                TextBox_Organ.Enabled = false;
                DropDownList_CostItems.Enabled = false;

            }





        }

        //新增加
        protected void Button_Add_Click(object sender, EventArgs e)
        {


            // Label_FatherId.Text = Label_CenterID.Text;//当新增一行记录的时候，对当前ID自动填为父ID。
            Label_CenterID.Text = "";
            TextBox_FatherId.Text = "";
            TextBox_Name.Text = "";
            TextBox_Description.Text = "";
            DropDownList_Sort.SelectedIndex = -1;
            CheckBox_DirectCost.Checked = false;
            CheckBox_StopFlag.Checked = false;
            TextBox_Organ.Text = "";


            EditFlag.Value = "add";  //将隐藏字段的值改为增加状态。
            EditState(true);




        }
        //byj 
        protected void Button_Edit_Click(object sender, EventArgs e)
        {

            if (EditFlag.Value == "Browse")
            {

                EditFlag.Value = "edit";//将隐藏字段的值改为编辑状态。
                EditState(true);
            }

        }
        //
        protected void Button_Del_Click(object sender, EventArgs e)
        {
            if (EditFlag.Value == "Browse")
            {

                EditFlag.Value = "edit";
                EditState(true);
            }


            int MyCenterId = Convert.ToInt32(Label_CenterID.Text);

            var QueryHaveChildCount = (from b in db.TABC_JS_CostCenter where b.上级作业中心ID == MyCenterId select b).Count();

            if (QueryHaveChildCount != 0)
            {
                Response.Write("<script>alert('该作业中心存在下属作业中心，不能删除');</script>");                
                return;                      
            }

            var HasPIP = from m in db.TABC_JS_ProductInProcess
                         where m.作业中心ID == MyCenterId
                         select m;
            if (HasPIP.Count() != 0)
            {

                Response.Write("<script>alert('该作业中心已定义有产品中间体，不能删除');</script>");
                return; 
            
            }

            var MyCenterQuery = (from a in db.TABC_JS_CostCenter where a.作业中心ID == MyCenterId select a).Single();

            db.TABC_JS_CostCenter.DeleteOnSubmit(MyCenterQuery);
            db.SubmitChanges();

            CreateTree();


        }
        //
        protected void Button_Save_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('非法数据输入,请修改'); </script>");
                return;

            }

            if (EditFlag.Value == "edit")
            {


                int MyCenterId = Convert.ToInt32(Label_CenterID.Text);
                var MyCenterQuery = (from a in db.TABC_JS_CostCenter where a.作业中心ID == MyCenterId select a).Single();


                MyCenterQuery.上级作业中心ID = Convert.ToInt32(TextBox_FatherId.Text);
                MyCenterQuery.作业中心名称 = TextBox_Name.Text;
                MyCenterQuery.作业中心描述 = TextBox_Description.Text;
                MyCenterQuery.类型 = DropDownList_Sort.SelectedValue;
                MyCenterQuery.是否直接成本 = CheckBox_DirectCost.Checked;
                MyCenterQuery.停用 = CheckBox_StopFlag.Checked;
                MyCenterQuery.部门ID = TextBox_Organ.Text;
                MyCenterQuery.成本细项ID = DropDownList_CostItems.SelectedValue;
                try
                {
                    db.SubmitChanges();
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('修改成功'); </script>");


                    EditFlag.Value = "Browse";
                    EditState(false);
                    CreateTree();

                }
                catch
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('修改失败'); </script>");
                }




            }
            else if (EditFlag.Value == "add")
            {
                //判断当前输入的上级作业中心是否存在，不存在则跳出
                int Fatherid = (from b in db.TABC_JS_CostCenter where b.作业中心ID == Convert.ToInt32(TextBox_FatherId.Text) select b).Count();
                if (Fatherid != 1)
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('上级作业中心无效'); </script>");
                    return;
                }


                TABC_JS_CostCenter MyRow = new TABC_JS_CostCenter();
                MyRow.公司ID = "hisun";
                MyRow.账套ID = "001";
                MyRow.上级作业中心ID = Convert.ToInt32(TextBox_FatherId.Text);
                MyRow.作业中心名称 = TextBox_Name.Text;
                MyRow.作业中心描述 = TextBox_Description.Text;
                MyRow.类型 = DropDownList_Sort.SelectedValue;
                MyRow.是否直接成本 = CheckBox_DirectCost.Checked;
                MyRow.停用 = CheckBox_StopFlag.Checked;
                MyRow.部门ID = TextBox_Organ.Text;
                MyRow.成本细项ID = DropDownList_CostItems.SelectedValue;

                try
                {
                    db.TABC_JS_CostCenter.InsertOnSubmit(MyRow);
                    db.SubmitChanges();
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('增加成功'); </script>");

                    EditFlag.Value = "Browse";
                    EditState(false);
                    CreateTree();
                }
                catch
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('增加失败'); </script>");
                }

            }


        }
        //
        protected void Button_Cancel_Click(object sender, EventArgs e)
        {
            if (EditFlag.Value != "Browse")
            {

                EditFlag.Value = "Browse";//将隐藏字段的值改为浏览状态。
                EditState(false);

            }



        }




    }
}
